package com.zx.sercurity;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;

public class VPNRisk implements RiskControlInterface<RiskResult.VPNRiskResult> {
    @Override
    public RiskResult.VPNRiskResult riskCheck(Context context) {
        return isUsingVPN(context) ? new RiskResult.VPNRiskResult(true) : new RiskResult.VPNRiskResult(false);
    }

    // 检测是否使用了 VPN
    public static boolean isUsingVPN(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            Network activeNetwork = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                activeNetwork = connectivityManager.getActiveNetwork();
            }
            if (activeNetwork != null) {
                NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(activeNetwork);
                if (capabilities != null) {
                    return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
                }
            }
        }
        return false;
    }
}
